package com.ruoyi.wms.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.utils.MapstructUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.wms.domain.bo.partolStore.InspectionPlanBo;
import com.ruoyi.wms.domain.entity.partolStore.InspectionPlan;
import com.ruoyi.wms.domain.vo.InspectionPlanVo;
import com.ruoyi.wms.mapper.InspectionPlanMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 巡检方案Service业务层处理
 *
 * @author 邓滔滔
 * @date 2024-11-24
 */
@RequiredArgsConstructor
@Service
public class InspectionPlanService {

    private final InspectionPlanMapper inspectionPlanMapper;

    /**
     * 查询巡检方案
     */
    public InspectionPlanVo queryById(Long id) {
        return inspectionPlanMapper.selectVoById(id);
    }

    /**
     * 查询巡检方案列表
     */
    public TableDataInfo<InspectionPlanVo> queryPageList(InspectionPlanBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<InspectionPlan> lqw = buildQueryWrapper(bo);
        Page<InspectionPlanVo> result = inspectionPlanMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询巡检方案列表
     */
    public List<InspectionPlanVo> queryList(InspectionPlanBo bo) {
        LambdaQueryWrapper<InspectionPlan> lqw = buildQueryWrapper(bo);
        return inspectionPlanMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<InspectionPlan> buildQueryWrapper(InspectionPlanBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<InspectionPlan> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getEquipmentType()), InspectionPlan::getEquipmentType, bo.getEquipmentType());
        lqw.between(params.get("beginNextInspectionDate") != null && params.get("endNextInspectionDate") != null,
            InspectionPlan::getNextInspectionDate, params.get("beginNextInspectionDate"), params.get("endNextInspectionDate"));
        lqw.eq(StringUtils.isNotBlank(bo.getInspectionItems()), InspectionPlan::getInspectionItems, bo.getInspectionItems());
        lqw.eq(bo.getStatus() != null, InspectionPlan::getStatus, bo.getStatus());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            InspectionPlan::getCreateTime, params.get("beginCreateTime"), params.get("endCreateTime"));
        lqw.between(params.get("beginUpdateTime") != null && params.get("endUpdateTime") != null,
            InspectionPlan::getUpdateTime, params.get("beginUpdateTime"), params.get("endUpdateTime"));
        return lqw;
    }


    /**
     * 新增巡检方案
     */
    public void insertByBo(InspectionPlanBo bo) {
        InspectionPlan add = MapstructUtils.convert(bo, InspectionPlan.class);
        inspectionPlanMapper.insert(add);
    }

    /**
     * 修改巡检方案
     */
    public void updateByBo(InspectionPlanBo bo) {
        InspectionPlan update = MapstructUtils.convert(bo, InspectionPlan.class);
        inspectionPlanMapper.updateById(update);
    }

    /**
     * 批量删除巡检方案
     */
    public void deleteByIds(Collection<Long> ids) {
        inspectionPlanMapper.deleteBatchIds(ids);
    }
}
